package com.example.yan_music.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.example.yan_music.R;
import com.example.yan_music.app.XApplication;
import com.example.yan_music.bean.wangyi.Banner;
import com.google.android.material.shape.RoundedCornerTreatment;
import com.youth.banner.adapter.BannerAdapter;

import java.util.List;
import java.util.Random;

/**
 * <p>
 *      第三方库Banner 的适配器
 * </>
 */
public class BannerImageAdapter extends BannerAdapter<Banner.BannersBean, BannerImageAdapter.BannerViewHolder> {

    private static final String TAG = "BannerImageAdapter";

    /**
     * 从服务器请求的到的数据
     */
    private List<Banner.BannersBean> mBannerList;

    /**
     * 对应网抑云Banner标题背景，根据不同性质的事件设置不同的背景色
     */
    private int colorRes[] = {
            R.drawable.shape_banner_title,
            R.drawable.shape_banner_title_1,
            R.drawable.shape_banner_title_2,
    };

    public BannerImageAdapter(List<Banner.BannersBean> datas) {
       super(datas);
    }

    @Override
    public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_banner_new, parent, false);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        return new BannerViewHolder(view);
    }

    @Override
    public void onBindView(BannerViewHolder holder, Banner.BannersBean data, int position, int size) {
        // 封面url
        String imgUrl = data.getPic();
        //Log.d(TAG, "onBindView: " + imgUrl);
        // 标题
        String bannerTip = data.getTypeTitle();
        //holder.img_banner.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(XApplication.getContext())
                .load(imgUrl)
                .transform(new GlideRoundTransform(XApplication.getContext()))
                .into(holder.img_banner);
        holder.tv_banner_tip.setText(bannerTip);
        holder.tv_banner_tip.setBackgroundResource(getRandomBackgroundColor());
    }

    /**
     * Banner的ViewHolder
     */
    class BannerViewHolder extends RecyclerView.ViewHolder {
        private ImageView img_banner;
        private TextView tv_banner_tip;

        public BannerViewHolder(@NonNull View itemView) {
            super(itemView);
            this.img_banner = itemView.findViewById(R.id.img_banner_bac);
            this.tv_banner_tip = itemView.findViewById(R.id.tv_banner_tip);
        }
    }

    /**
     * 使用Glide为ImageView加载圆角图片，解决XML设置不生效的问题
     */
    public static class GlideRoundTransform extends BitmapTransformation {

        private  float radius = 0f;

        public GlideRoundTransform(Context context) {
            this(context, 10);
        }

        /**
         * 构造器
         * @param context
         * @param dp    圆角dp
         */
        public GlideRoundTransform(Context context, int dp) {
            super(context);
            this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
        }

        @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            return roundCrop(pool, toTransform);
        }

        private  Bitmap roundCrop(BitmapPool pool, Bitmap source) {
            if (source == null) {
                return null;
            }

            Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
            if (result == null) {
                result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
            }

            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
            paint.setAntiAlias(true);
            RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
            canvas.drawRoundRect(rectF, radius, radius, paint);
            return result;
        }

        @Override public String getId() {
            return getClass().getName() + Math.round(radius);
        }
    }

    /**
     * 获取随机的背景色
     * @return
     */
    private int getRandomBackgroundColor(){
        Random random = new Random();
        return colorRes[random.nextInt(3)];
    }
}
